package myJs.user.workflow.featureSelection.pathwayAna

import com.softwaremill.quicklens._
import myJs.cps._
import myJs.myPkg.reactHookForm._
import myJs.tool.Tool._
import myJs.user.workflow.featureSelection.FileSelectCp
import shared.pojo.WorkflowMission._

import scala.collection.immutable.SeqMap
import shared.VarTool
import myJs.Implicits._
import myJs.tool.Tool
import myJs.user.soft.keggEnrich.KeggEnrich.speciesData

import scala.scalajs.js

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.Utils._
import myJs.user.soft.keggEnrich.KeggEnrich

/** Created by yz on 3/11/2020
  */
object PathwayAnaCp {

  case class Props(
      curI: Int,
      data: EachPathwayAnaData,
      dataChange: EachPathwayAnaData => SyncIO[Unit],
      dataModifyF: (EachPathwayAnaData => EachPathwayAnaData) => SyncIO[Unit],
      extraData: ExtraData,
      extraDataChange: ExtraData => SyncIO[Unit],
      fsData: FsData
  ) {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val curTool = PathwayAna
    val namePrefix = s"pathwayAna.${props.curI}"
    val basicData = props.data
    object FTool {

      def fcMethodChange = (v: String) => {
        val newData = props.data.modify(_.fcMethod).setTo(v)
        props.dataChange(newData)
      }

      def execIPathChange = (v: Boolean) => {
        val newData = props.data.modify(_.execIPath).setTo(v)
        props.dataChange(newData)
      }

      def libTypeChange = (v: String) => {
        val newData = props.data.modify(_.libType).setTo(v)
        props.dataChange(newData)
      }

      def species1ModifyF = (v: String) => {
        val newData = modify(_: EachPathwayAnaData)(_.species1).setTo(v)
        props.dataModifyF(newData)
      }

      def speciesModifyF = (v: String) => {
        val newData = modify(_: EachPathwayAnaData)(_.species).setTo(v)
        props.dataModifyF(newData)
      }

      def isSmpModifyF = (v: String) => {
        val newData = modify(_: EachPathwayAnaData)(_.isSmp).setTo(v)
        props.dataModifyF(newData)
      }

      def keggEnrichMethodChange = (v: String) => {
        val newData = props.data.modify(_.method).setTo(v)
        props.dataChange(newData)
      }

      def nodeImpChange = (v: String) => {
        val newData = props.data.modify(_.nodeImp).setTo(v)
        props.dataChange(newData)
      }

    }
    val formState = ReactHookForm.useFormState(options =
      UseFormStateOptions(
        name = namePrefix
      )
    )
    useEffect(
      SyncIO {
        Tool.refreshNodeColor(props.data.nodeId, formState, namePrefix)
      },
      List(formState)
    )

    useEffect(
      {
        FTool.species1ModifyF(props.fsData.dataMatrix.species1)
      },
      List(props.fsData.dataMatrix.species1)
    )

    useEffect(
      {
        FTool.speciesModifyF(props.fsData.dataMatrix.species)
      },
      List(props.fsData.dataMatrix.species)
    )

    useEffect(
      {
        FTool.isSmpModifyF(props.fsData.dataMatrix.isSmp)
      },
      List(props.fsData.dataMatrix.isSmp)
    )

    val fcMethodMap = SeqMap("median" -> "Median", "mean" -> "Mean", "auto" -> messages("wiseChoose"))
    val isMul = props.extraData.groups.size > 2
    val species1Map = KeggEnrich.speciesData.keyList.optionMap
    val speciesMap = speciesData(basicData.species1)
    val isSmpMap = if (List("hsa", "mmu").contains(basicData.species)) {
      SeqMap("kegg" -> "KEGG", "smp" -> "SMPDB")
    } else SeqMap("kegg" -> "KEGG")
    val keggEnrichMethodMap = SeqMap("hyperg" -> "Hypergeometric Test", "fisher" -> "Fisher's Exact Test")
    val nodeImpMap = SeqMap("rbc" -> "Relative-betweeness Centrality", "dgr" -> "Out-degree Centrality")
    val displayStr = if (!isMul) "block" else "none"

    div(
      SelectCp
        .Props(
          value = basicData.fcMethod,
          label = messages("fCCalculationMethod"),
          name = s"${namePrefix}.fcMethod",
          optionMap = fcMethodMap,
          onChange = FTool.fcMethodChange,
          questionInfo = messages("fCCalculationMethodInfo"),
          show = !isMul
        )
        .render,
      div(display := displayStr, className := "form-group col-sm-12", hr()),
      h4(messages("iPathAnalysis")),
      ToggleCp
        .Props(
          value = basicData.execIPath,
          label = messages("runIPathAnalysis"),
          name = s"${namePrefix}.execIPath",
          valueChange = FTool.execIPathChange
        )
        .render,
      div(className := "form-group col-sm-12", hr()),
      h4(messages("msea")),
      RSelectCp
        .Props(
          value = basicData.libType,
          label = "metabolite set library",
          name = s"${namePrefix}.libType",
          optionMap = VarTool.libTypeMap,
          onChange = FTool.libTypeChange,
          questionInfo = messages("libraryInfo")
        )
        .render,
      div(className := "form-group col-sm-12", hr()),
      h4(
        messages("metPA"),
        span(
          className := "",
          a(
            `type` := "button",
            className := "selfQuestion",
            title := messages("note"),
            dataContainer := "body",
            dataToggle := "popover",
            dataHtml := "true",
            dataPlacement := "left",
            dataContent := messages("metPAInfo"),
            dataTrigger := "hover",
            i(className := "fa fa-question-circle-o")
          )
        )
      ),
      div(
       paddingRight := "0px", paddingLeft := "0px",
        className := "form-group col-sm-12",
        label(className := s"", s"${messages("species")}", ":"),
        PureSelectCp.Props(
          name = s"${namePrefix}.species1",
          value = basicData.species1,
          onChange = FTool.species1ModifyF,
          optionMap = species1Map
        ).render,
        div(
         marginTop := 5.px,
          PureSelectCp.Props(
            name = s"${namePrefix}.species",
            value = basicData.species,
            onChange = FTool.speciesModifyF,
            optionMap = speciesMap
          ).render
        ),
        div(
          marginTop := 5.px,
          PureSelectCp.Props(
            name = s"${namePrefix}.isSmp",
            value = basicData.isSmp,
            onChange = FTool.isSmpModifyF,
            optionMap = isSmpMap
          ).render
        )
      ),
      SelectCp.Props(
        value = basicData.method,
        label = "Over Representation Analysis",
        name = s"${namePrefix}.method",
        optionMap = keggEnrichMethodMap,
        onChange = FTool.keggEnrichMethodChange
      ).render,
      SelectCp.Props(
        value = basicData.nodeImp,
        label = "Pathway Topology Analysis",
        name = s"${namePrefix}.nodeImp",
        optionMap = nodeImpMap,
        onChange = FTool.nodeImpChange,
        questionInfo = messages("topologyInfo")
      ).render
    )

  }

}
